package net.ib.mtalk.util;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


public class ColorSlider extends View 
{
	private static final String LOG_TAG = "ColorSlider";
    private static final boolean debug = true;

    private Paint 		mPaint;
    private Shader 		mShader;
    private int []		mColors;
    private float		mPortion;
    private PorterDuffXfermode	xmode;
    
    private OnSliderColorChangedListener	mListener;
    
    public interface OnSliderColorChangedListener 
    {
        void colorChanged(ColorSlider slider, int color, float portion);
    }    
	
	public ColorSlider(Context context, AttributeSet attrs, int defStyle) 
	{
		super(context, attrs, defStyle);
		init();
	}
	public ColorSlider(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		init();
	}
	public ColorSlider(Context context) 
	{
		super(context);
		init();
	}
	
    void init() 
    {
    	mColors = new int[2];
    	mColors[0] = 0xFF000000;
    	mColors[1] = 0xFFFFFFFF;
    	mPortion = 1.0f;
		
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setStyle(Paint.Style.FILL);
		
		mShader = new LinearGradient( 0, 0, getWidth(), 0, mColors[0], mColors[1], Shader.TileMode.CLAMP );
		xmode = new PorterDuffXfermode( PorterDuff.Mode.XOR );
    }
    
    
    @Override 
    protected void onDraw(Canvas canvas) 
    {
        mPaint.setShader(mShader);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
        
        // draw selection
        float		xpos;
        
        xpos = mPortion * getWidth();
        mPaint.setShader( null );
        mPaint.setColor( 0xffffffff );
        mPaint.setStyle( Paint.Style.STROKE );
        mPaint.setXfermode( xmode );
        canvas.drawRect( xpos-1, 0, xpos+1, getHeight()-1, mPaint );
        canvas.drawRect( xpos-2, 1, xpos+2, getHeight()-2, mPaint );
        mPaint.reset();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(GraphicUtil.DipToPixel(ColorPickerDialog.parent_activity, ColorSelector.CENTER_X)*2
        		             ,GraphicUtil.DipToPixel(ColorPickerDialog.parent_activity, ColorSelector.STROKE_WIDTH));
    }
    
    public void setColors(int color1, int color2) 
    {
    	mColors = null;
    	mColors = new int[2];
    	mColors[0] = color1;
    	mColors[1] = color2;
    	
    	if ( debug )
    		Log.d(LOG_TAG, "setColors : " + getWidth() + "-" + getHeight());
        
        mShader = null; 
        mShader = new LinearGradient( 0, 0, getWidth(), 0, mColors[0], mColors[1], Shader.TileMode.CLAMP );
    
        invalidate();
    }
    
    public void setColors ( int[] colors )
    {
    	mColors = null;
    	mColors = colors.clone( );
    	
    	if ( debug )
    		Log.d(LOG_TAG, "setColors : " + getWidth() + "-" + getHeight());
    	
    	mShader = null;
    	mShader = new LinearGradient(0, 0, getWidth(), 0, mColors, null, Shader.TileMode.CLAMP );
    	
    	invalidate();
    }

    public void setSliderColorChangedListener( OnSliderColorChangedListener listener ) 
    {
    	mListener = listener;
    }
    
    private int ave(int s, int d, float p) 
    {
        return s + Math.round(p * (d - s));
    }

    private int interpColor(int[] colors, float unit) 
    {
        if (unit <= 0) {
            return colors[0];
        }
        if (unit >= 1) {
            return colors[colors.length - 1];
        }
        
        float p = unit * (colors.length - 1);
        int i = (int)p;
        p -= i;

        // now p is just the fractional part [0...1) and i is the index
        int c0 = colors[i];
        int c1 = colors[i+1];
        int a = ave(Color.alpha(c0), Color.alpha(c1), p);
        int r = ave(Color.red(c0), Color.red(c1), p);
        int g = ave(Color.green(c0), Color.green(c1), p);
        int b = ave(Color.blue(c0), Color.blue(c1), p);
        
        return Color.argb(a, r, g, b);
    }
    int getPortionColor ( )
    {
    	return interpColor(mColors, mPortion);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) 
    {
        float x = event.getX();
        //float y = event.getY();
        
        switch (event.getAction()) 
        {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                
                float 	unit = (float) x / ((float) getWidth()); 
                int 	newcolor = interpColor(mColors, unit);
                
                if (mListener != null) 
                {
                	mListener.colorChanged(this, newcolor, unit);
                }
                mPortion = unit;
                invalidate();
                break;
                
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }
    
    @Override
    protected void onSizeChanged (int w, int h, int oldw, int oldh)
    {
    	super.onSizeChanged(w, h, oldw, oldh);
    	
    	if ( oldw == 0 && oldh == 0 )
    	{
    		// this is a trick to generate shader...
    		// until now, don't know inner process of when owner dialog has finished layout...
        	mShader = null;
        	mShader = new LinearGradient(0, 0, getWidth(), getHeight(), mColors, null, Shader.TileMode.CLAMP );
    	}
    }
    
}
